<?php

namespace djepo\UserBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use djepo\UserBundle\Entity\Product;
use djepo\UserBundle\Entity\ProductAction;
use djepo\UserBundle\Entity\ProductExtended;
use djepo\UserBundle\Entity\Notification;
use djepo\UserBundle\Form\ProductType;

use Symfony\Component\HttpFoundation\Response;
use \DateTime;

/**
 * Product controller.
 *
 * @Route("/api/product")
 */
class ProductUserRestController extends Controller
{

    /**
     * Get user products
     *
     * @Route("/streamuser")
     * @Method("POST")
     * @Template()
     */
    public function streamuserloadAction()
    {
        $em = $this->getDoctrine()->getManager();
        $id = $this->get('request')->query->get('id');
        $user = $em->getRepository('djepoUserBundle:FosUser')->find($id);
        $serializer = $this->get('serializer'); 

        
        $qb = $em->createQueryBuilder();
        $qb->select('p');
        $qb->from('djepoUserBundle:ProductExtended','p');
        $qb->where('p.user = :user');
        $qb->setParameter('user',$user);
        $qb->orderBy('p.id', 'DESC');
        $entities = $qb->getQuery()->getResult();
        for ($i=0; $i < count($entities); $i++) { 
            $qbA = $em->createQueryBuilder();
            $qbA->select('pa');
            $qbA->from('djepoUserBundle:ProductAction','pa');
            $qbA->where('pa.product = :product');
            $qbA->setParameter('product',$entities[$i]);
            $actions = $qbA->getQuery()->getResult();
            $nbrBonneAffaire = 0;
            $bonneaffaire = false;
            $nbrJelai = 0;
            $jelai = false;
            for ($j=0; $j < count($actions); $j++){
                if($actions[$j]->getAction() == 0){
                    $nbrBonneAffaire++;
                    if($actions[$j]->getUser()->getId() == $user->getId()) $bonneaffaire = true;
                }
                else{
                    $nbrJelai++;
                    if($actions[$j]->getUser()->getId() == $user->getId()) $jelai = true;
                }
            }
            $entities[$i]->bonneaffaire = $bonneaffaire;
            $entities[$i]->nbrbonneaffaire = $nbrBonneAffaire;
            $entities[$i]->jelai = $jelai;
            $entities[$i]->nbrjelai = $nbrJelai;
        }

        return new Response($serializer->serialize($entities  , 'json')); 
    }

    /**
     * Get user products
     *
     * @Route("/streamuseraction")
     * @Method("POST")
     * @Template()
     */
    public function streamuseractionloadAction()
    {
        $em = $this->getDoctrine()->getManager();
        $id = $this->get('request')->query->get('id');
        $action = $this->get('request')->query->get('action');
        $user = $em->getRepository('djepoUserBundle:FosUser')->find($id);
        $serializer = $this->get('serializer'); 

        
        $qb = $em->createQueryBuilder();
        $qb->select('p');
        $qb->from('djepoUserBundle:ProductAction','pa');
        $qb->from('djepoUserBundle:ProductExtended','p');
        $qb->where('pa.user = :user');
        $qb->setParameter('user',$user);
        $qb->andWhere('pa.product = p');
        $qb->andWhere('pa.action = :action');
        $qb->setParameter('action',$action);
        $qb->orderBy('p.id', 'DESC');
        $entities = $qb->getQuery()->getResult();
        for ($i=0; $i < count($entities); $i++) { 
            $qbA = $em->createQueryBuilder();
            $qbA->select('pa');
            $qbA->from('djepoUserBundle:ProductAction','pa');
            $qbA->where('pa.product = :product');
            $qbA->setParameter('product',$entities[$i]);
            $actions = $qbA->getQuery()->getResult();
            $nbrBonneAffaire = 0;
            $bonneaffaire = false;
            $nbrJelai = 0;
            $jelai = false;
            for ($j=0; $j < count($actions); $j++){
                if($actions[$j]->getAction() == 0){
                    $nbrBonneAffaire++;
                    if($actions[$j]->getUser()->getId() == $user->getId()) $bonneaffaire = true;
                }
                else{
                    $nbrJelai++;
                    if($actions[$j]->getUser()->getId() == $user->getId()) $jelai = true;
                }
            }
            $entities[$i]->bonneaffaire = $bonneaffaire;
            $entities[$i]->nbrbonneaffaire = $nbrBonneAffaire;
            $entities[$i]->jelai = $jelai;
            $entities[$i]->nbrjelai = $nbrJelai;
        }

        return new Response($serializer->serialize($entities  , 'json')); 
    }

    /**
     * Get user products
     *
     * @Route("/nbruseraction")
     * @Method("POST")
     * @Template()
     */
    public function nbruseractionloadAction()
    {
        $em = $this->getDoctrine()->getManager();
        $id = $this->get('request')->query->get('id');
        $user = $em->getRepository('djepoUserBundle:FosUser')->find($id);
        $serializer = $this->get('serializer'); 

        
        $qb = $em->createQueryBuilder();
        $qb->select('pa');
        $qb->from('djepoUserBundle:ProductAction','pa');
        $qb->where('pa.user = :user');
        $qb->setParameter('user',$user);
        $qb->andWhere('pa.action = 0');
        $nbrBonaffaire = count($qb->getQuery()->getResult());

        $qb = $em->createQueryBuilder();
        $qb->select('pa');
        $qb->from('djepoUserBundle:ProductAction','pa');
        $qb->where('pa.user = :user');
        $qb->setParameter('user',$user);
        $qb->andWhere('pa.action = 1');
        $nbrJelai = count($qb->getQuery()->getResult());
    

        return new Response($nbrBonaffaire."_".$nbrJelai); 
    }

    
    
}